package com.qsl.ssyx.activity.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsl.ssyx.activity.service.ActivityInfoService;
import com.qsl.ssyx.client.product.ProductFeignClient;
import com.qsl.ssyx.common.result.Result;
import com.qsl.ssyx.model.activity.ActivityInfo;
import com.qsl.ssyx.model.activity.ActivityRule;
import com.qsl.ssyx.model.activity.ActivitySku;
import com.qsl.ssyx.model.product.SkuInfo;
import com.qsl.ssyx.vo.activity.ActivityRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author 青衫泪
 * @since 2023-08-28
 */
@Api(tags = "营销活动接口")
@RestController
@RequestMapping("/admin/activity/activityInfo")
public class ActivityInfoController {

    @Autowired
    private ActivityInfoService activityInfoService;
    @Autowired
    private ProductFeignClient productFeignClient; // 服务调用

    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result selectPage(@ApiParam(value = "当前页", required = true) @PathVariable Integer page
            , @ApiParam(value = "每页记录数", required = true) @PathVariable Integer limit) {
        Page<ActivityInfo> pageParam = new Page<>(page, limit);
        IPage<ActivityInfo> model = activityInfoService.selectPage(pageParam);
        return Result.ok(model);
    }

    @ApiOperation("新增")
    @PostMapping("save")
    public Result save(@RequestBody ActivityInfo activityInfo) {

        boolean flag = activityInfoService.save(activityInfo);
        return Result.ok(flag);
    }

    @ApiOperation("根据Id查询")
    @GetMapping("get/{id}")
    public Result getById(@ApiParam(value = "ActivityInfoId", required = true)
                          @PathVariable("id") Integer ActivityInfoId) {
        ActivityInfo activityInfo = activityInfoService.getById(ActivityInfoId);
        return Result.ok(activityInfo);
    }

    @ApiOperation("修改")
    @PutMapping("update")
    public Result update(@RequestBody ActivityInfo activityInfo) {
        boolean flag = activityInfoService.updateById(activityInfo);
        return Result.ok(flag);
    }

    @ApiOperation("根据ActivityInfoId删除")// activity_info,activity_rule,activity_sku
    @DeleteMapping("remove/{Id}")
    public Result remove(@ApiParam("ActivityInfoId") @PathVariable("Id") Integer ActivityInfoId) {

        boolean flag = activityInfoService.removeByActivityInfoId(ActivityInfoId);
        return Result.ok(flag);
    }

    /* =========================规则 ========================= */
    /* ============营销活动规则==============*/
    @ApiOperation("根据营销活动Id 营销活动规则和活动范围")
    @GetMapping("findActivityRuleList/{id}")
    public Result getActivityRuleById(@ApiParam(value = "ActivityInfoId", required = true)
                                      @PathVariable("id") Integer ActivityInfoId) {
        Map<String, Object> map = activityInfoService.getActivityRuleById(ActivityInfoId);
        return Result.ok(map);
    }

    @ApiOperation("根据商品名称查询-saveActivityRule活动参与商品")
    @GetMapping("findSkuInfoByKeyword/{Keyword}")
    public Result findSkuInfoByKeyword(@ApiParam(value = "商品名称", required = true)
                                       @PathVariable("Keyword") String skuName) {
        List<SkuInfo> skuInfoList = productFeignClient.getSkuInfosBySkuName(skuName);
        return Result.ok(skuInfoList);
    }

    @ApiOperation("新增规则") //
    @PostMapping("saveActivityRule")
    public Result saveActivityRule(@RequestBody ActivityRuleVo activityRuleVo) {

        boolean flag = activityInfoService.saveActivityRule(activityRuleVo);
        return Result.ok(flag);
    }

}

